<template>
  <div class="bgc-default pad-20">
    <div class="bg-color-white pad-20">
      <!--顶部搜索 -->
      <div class="top-operate-default">
        <div class="search-item">
          <label>搜索优惠券：</label>
          <el-select v-model="coupon.textCol">
            <el-option label="券码" value="couponNo"></el-option>
          </el-select>
          <el-input style="width: 200px;" v-model="coupon.textValue" placeholder="请输入内容"></el-input>
          <label class="mar-l-20">优惠券类型：</label>
          <el-select v-model="coupon.couponType" placeholder="优惠券类型">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(val, key) in dict.couponType" :key="key" :label="val" :value="key"/>
          </el-select>
          <label class="mar-l-20">范围查询：</label>
          <el-select v-model="coupon.ratioCol">
            <el-option label="领取率" value="receiveRatio"></el-option>
            <el-option label="使用率" value="useRatio"></el-option>
            <el-option label="优惠率" value="reduceRatio"></el-option>
          </el-select>
          <el-input v-model="coupon.ratioStart" placeholder="请输入数值" style="width: 150px;" maxlength="5" @keyup.native="coupon.ratioStart = checkInputNum(coupon.ratioStart)"></el-input>
          <span class="mar-l-5 mar-r-5">至</span>
          <el-input v-model="coupon.ratioEnd" placeholder="请输入数值" style="width: 150px;" maxlength="5" @keyup.native="coupon.ratioEnd = checkInputNum(coupon.ratioEnd)"></el-input>
          <el-button-group class="mar-l-20">
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button plain="" @click="resetSearch">重置</el-button>
          </el-button-group>          
          <!-- <div class="pad-t-10">
            <el-button type="primary" @click="exportData">导出</el-button>
          </div> -->
        </div>
      </div>
      <!-- 全选、操作 -->
      <div>
        <div class="pad-t-15 pad-b-15 font-14 v-align-c">
          <el-checkbox class="mar-l-14 mar-r-0" v-model="allChecked" @change="selectAll"><span class="mar-l-20">当页全选</span></el-checkbox>
          <span class="pad-l-4 pad-r-4">|</span>
          <div class="width-80">已选{{multipleSelection.length}}项</div>
          <el-button class="batch-btn" @click="exportData">批量导出</el-button>
        </div>
      </div>
      <!-- 内容列表 -->
      <div class="table-container">
        <el-table ref="multipleTable" :data="couponList" v-loading="listLoading" element-loading-text="加载中" style="width: 100%" 
          header-cell-class-name="default-header" row-class-name="default-row" @selection-change="handleSelectionChange">
          <el-table-column width="55px" type="selection" ></el-table-column>
          <el-table-column width="200px" label="券码"  prop="couponNo"></el-table-column>
          <el-table-column width="180px" label="券名称"  prop="couponName"></el-table-column>
          <el-table-column width="100px" label="券类型"  prop="couponType">
            <template slot-scope="scope">
              <div v-if="scope.row.couponType" class="font-14">
                <div style="font-size: 14px;">{{dict.couponType[scope.row.couponType]}}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="200px" label="优惠内容" prop="couponRemark"></el-table-column>
          <el-table-column min-width="200px" label="使用期限">
            <template slot-scope="scope">
              <div v-if="scope.row.validTimeType == 1">
                <span>开始：{{scope.row.validStartTime}}</span><br/>
                <span>结束：{{scope.row.validEndTime}}</span>
              </div>
              <div v-if="scope.row.validTimeType == 2">领券当日起{{scope.row.validFromToday||''}}天内有效</div>
              <div v-if="scope.row.validTimeType == 3">领券次日起{{scope.row.validFromTomorrow||''}}天内有效</div>
            </template>
          </el-table-column>
          <el-table-column width="100px" label="发放数量">
            <template slot-scope="scope">
              <div>{{scope.row.couponNum||'不限数量'}}</div>
            </template>
          </el-table-column>
          <el-table-column width="150px" label="领取数量">
            <template slot-scope="scope">
              <div class="flex">
                <div class="font-color-9 width-56 text-align-r">数量：</div>
                <span>{{scope.row.receiveNum||'0'}}</span>
              </div>
              <div v-if="!!scope.row.couponNum" class="flex">
                <div class="font-color-9 width-56 text-align-r">领取率：</div>
                <span>{{scope.row.receiveRatio}}%</span>
              </div>
              <div v-else class="flex">
                <div class="font-color-9 width-56 text-align-r">领取率：</div>
                <span>100%</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="150px" label="使用数量">
            <template slot-scope="scope">
              <div class="flex">
                <div class="font-color-9 width-56 text-align-r">数量：</div>
                <span>{{scope.row.usedNum||'0'}}</span>
              </div>
              <div v-if="!!scope.row.couponNum" class="flex">
                <div class="font-color-9 width-56 text-align-r">使用率：</div>
                <span>{{scope.row.useRatio}}%</span>
              </div>
              <div v-else class="flex">
                <div class="font-color-9 width-56 text-align-r">使用率：</div>
                <span>0%</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column width="150px" label="用券订单总金额" prop="orderTotalPrice">
            <template slot-scope="scope">￥{{scope.row.orderTotalPrice}}</template>
          </el-table-column>
          <el-table-column width="100px" label="优惠总金额" prop="reduceTotalAmount">
            <template slot-scope="scope">￥{{scope.row.reduceTotalAmount}}</template>
          </el-table-column>
          <el-table-column width="100px" label="优惠率" prop="reduceRatio">
            <template slot-scope="scope">
              <div v-if="scope.row.reduceRatio">{{scope.row.reduceRatio}}%</div>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <!-- 列表分页 -->
      <div class="pagination">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                       :current-page="searchListQuery.currentPage"
                       :page-sizes="[10, 20, 50, 100]" :page-size="searchListQuery.size"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
  .select-class {
    margin-right: -5px;
    width: 139px;
  }
</style>

<script>
  import { pagingCouponStat } from '@/api/coupon/coupon'
  import { downloadFile } from '@/api/common/common'

  export default {
    data() {
      return {
        total: 0,
        listLoading: true,
        searchListQuery: {},
        defaultQuery: {
          enterpriseId: this.getEnterpriseId(),
          couponName: '',
          couponType: '',
          currentPage: 1,
          size: 10
        },
        coupon: {
          textCol: 'couponNo',
          textValue: '',
          ratioCol: 'receiveRatio',
          ratioStart: '',
          ratioEnd: ''
        },
        endCouponVisible: false,
        editCouponInfo: {},
        dict: {
          couponStatus: {},
          couponType: {}
        },
        couponList: [],
        multipleSelection: [],
        allChecked: false
      }
    },
    // 初始化页面
    created() {
      this.searchListQuery = { ...this.defaultQuery }
      this.init()
    },
    mounted() {
    },
    methods: {
      // 更改当前页数
      handleCurrentChange(val) {
        this.searchListQuery.currentPage = val
        this.init()
      },
      // 更改当前页面大小
      handleSizeChange(val) {
        this.searchListQuery.size = val
        this.init()
      },
      setSearchListQuery() {
        // 搜索状态
        const currentPage = this.searchListQuery.currentPage
        const size = this.searchListQuery.size
        this.searchListQuery = { ... this.defaultQuery }
        this.searchListQuery.enterpriseId = this.getEnterpriseId()
        this.searchListQuery.currentPage = currentPage
        this.searchListQuery.size = size
        this.searchListQuery[this.coupon.textCol] = this.coupon.textValue
        this.searchListQuery.couponType = this.coupon.couponType
        if (this.coupon.ratioStart && this.coupon.ratioEnd) {
          this.searchListQuery[this.coupon.ratioCol + 'Start'] = this.coupon.ratioStart
          this.searchListQuery[this.coupon.ratioCol + 'End'] = this.coupon.ratioEnd
        }
      },
      init() {
        this.setSearchListQuery()
        this.listLoading = true
        pagingCouponStat(this.searchListQuery).then(response => {
          if (response.isError) return
          this.total = response.data.total
          this.couponList = response.data.records
          Object.assign(this.dict, response.data.dict)
          this.listLoading = false
        })
      },
      handleSearch() {
        this.searchListQuery.currentPage = 1
        this.searchListQuery.size = 10
        this.init()
      },
      resetSearch() {
        this.searchListQuery = { ... this.defaultQuery }
        this.coupon = {
          textCol: 'couponNo',
          textValue: '',
          ratioCol: 'receiveRatio',
          ratioStart: '',
          ratioEnd: ''
        }
        this.init()
      },
      exportData() {
        if (this.multipleSelection.length > 0) {
          const couponIds = this.multipleSelection.map(item => item.couponId)
          downloadFile('/sell/coupon/exportStatData', {
            enterpriseId: this.getEnterpriseId(),
            couponIds: couponIds.join(',')
          }, '券汇总报表.xlsx')
        } else {
          this.setSearchListQuery()
          downloadFile('/sell/coupon/exportStatData', this.searchListQuery, '券汇总报表.xlsx')
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
        if (this.multipleSelection.length === this.couponList.length) {
          this.allChecked = true
        } else {
          this.allChecked = false
        }
      },
      selectAll() {
        if (this.multipleSelection.length === 0 || this.multipleSelection.length < this.couponList.length) {
          this.couponList.forEach(item => {
            this.$refs.multipleTable.toggleRowSelection(item, true)
          })
        } else {
          this.$refs.multipleTable.clearSelection()
        }
      }
    }
  }

</script>
